/*
 * orderinfo_transfer.c
 *
 *  Created on: 2013-2-14
 *      Author: panqiuhui
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include "../head/communicate_with_server/orderinfo_transfer.h"

extern int socketfd_orderinfo;

void output_item(OrderItem item) {
	printf("--------------------------------\n");
	printf("orderID: %s\n", item.orderID);
	printf("trainID: %s\n", item.trainID);
	printf("compartmentNum: %s\n", item.compartmentNum);
	printf("seatNum: %s\n", item.seatNum);
	printf("name: %s\n", item.name);
	printf("creditType: %s\n", item.creditType);
	printf("creditID: %s\n", item.creditID);
	printf("departDate: %s\n", item.departDate);
	printf("departStation: %s\n", item.departStation);
	printf("arriveStation: %s\n", item.arriveStation);
	printf("price :%s\n", item.price);
	printf("--------------------------------\n");
}

//向服务器发送查询订单的请求，找到的话返回true并将结果保存在orderlist中，没找到的话返回false
bool search_order(const struct orderinfo_search_request *request,
		OrderList *orderlist) {
	bool result = true;
	struct orderinfo_request_type type;
	type.type = TYPE_SEARCH_ORDER;
	int write_return = write(socketfd_orderinfo, &type, sizeof(type));
	if (write_return == -1) {
		perror("orderinfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}
	write_return = write(socketfd_orderinfo, request, sizeof(*request));
	if (write_return == -1) {
		perror("orderinfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}

	int read_return = read(socketfd_orderinfo, &result, sizeof(result));
	if (read_return == -1) {
		perror("orderinfo_transfer.c: read error!\n");
		exit(EXIT_FAILURE);
	}
	if (result) {
		bool islast = false;
		while (!islast) {
			OrderItem item;
			memset(&item, 0, sizeof(item));
			read_return = read(socketfd_orderinfo, &item, sizeof(item));
			if (read_return == -1) {
				perror("orderinfo_transfer.c: read error!\n");
				break;
			}
			if (!AddItem(item, orderlist)) {
				break;
			}
			read_return = read(socketfd_orderinfo, &islast, sizeof(islast));
			if (read_return == -1) {
				perror("orderinfo_transfer.c: read error!\n");
				break;
			}
		}
	}
	return result;
}

//向服务器发送退票的请求，成功返回true，失败返回false;
bool refund_order(const struct orderinfo_refund_request *request,
		const OrderList *orderlist) {
	bool result = true;
	struct orderinfo_request_type type;
	type.type = TYPE_REFUND_ORDER;
	int write_return = write(socketfd_orderinfo, &type, sizeof(type));
	if (write_return == -1) {
		perror("orderinfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}
	write_return = write(socketfd_orderinfo, request, sizeof(*request));
	if (write_return == -1) {
		perror("orderinfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}

	OrderNode *node = *orderlist;
	bool islast = false;
	while (node != NULL ) {
		OrderItem item = node->item;
		write_return = write(socketfd_orderinfo, &item, sizeof(item));
		if (write_return == -1) {
			perror("orderinfo_transfer.c: write error!\n");
			result = false;
			break;
		}
		node = node->next;
		if (node == NULL ) {
			islast = true;
		}
		write(socketfd_orderinfo, &islast, sizeof(islast));
	}

	int read_return = read(socketfd_orderinfo, &result, sizeof(result));
	if (read_return == -1) {
		perror("orderinfo_transfer.c: read error!\n");
		exit(EXIT_FAILURE);
	}

	return result;
}
